package com.shop.service;

import com.alipay.api.domain.Car;
import com.shop.comment.Constants;
import com.shop.dao.BaseOrderDao;
import com.shop.dao.GoodsDao;
import com.shop.dao.OrderInfoDao;
import com.shop.dao.UserDao;
import com.shop.dao.impl.BaseOrderDaoImpl;
import com.shop.dao.impl.GoodsDaoImpl;
import com.shop.dao.impl.OrderInfoDaoImpl;
import com.shop.dao.impl.UserDaoImpl;
import com.shop.entity.BaseOrder;
import com.shop.entity.Goods;
import com.shop.entity.OrderInfo;
import com.shop.entity.User;
import com.shop.entity.VO.BuyVo;
import com.shop.entity.VO.CarVo;
import com.shop.entity.VO.GoodsParamVo;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Description TODO
 * @Author hyl
 * @Date 2022/3/11 15:20
 **/
public class GoodsServiceImpl implements GoodsService{
	GoodsDao goodsDao = new GoodsDaoImpl();
	UserDao userDao = new UserDaoImpl();
	@Override
	public List<Goods> findGoodByPage(int beginIndex, int showNum) {
		return goodsDao.findGoodByPage(beginIndex, showNum);
	}

	@Override
	public List<Goods> findGoodByPage(GoodsParamVo paramVo) {
		return goodsDao.findGoodByPage(paramVo);
	}

	@Override
	public Goods findById(Integer goodsId) {
		return goodsDao.findById(goodsId);
	}

	@Override
	public List<CarVo> findCarVo(String carInfo)//String car = "578-1_670-3_555-1"; //商品编号-数量
	{
		List<CarVo> list = new ArrayList<CarVo>();
		//Cookie / 缓存中 存放的数据==》String
		if(carInfo != null) {
			String[] goodsList = carInfo.split("_");//商品编号-数量
			if (goodsList.length > 0) {
				for (String tmp : goodsList) {
					String[] goodsInfo = tmp.split("-"); //0 商品编号 ， 1 数量
					Goods goods1 = goodsDao.findById(Integer.valueOf(goodsInfo[0]));
					CarVo vo = new CarVo(Integer.valueOf(goodsInfo[1]), goods1);
					list.add(vo);
				}
			}
		}
		return list;
	}

	@Override
	public List<CarVo> findCarVo(Integer loginUserId) {
		List<Car> userCar = null;//  dao中获取
		return null;
	}

	@Override
	public String buy(BuyVo buyVo) {
		List<OrderInfo> infoList = new ArrayList<OrderInfo>();
		//循环遍历需要购买的商品==》判断库存 ==》扣减库存 ==>t_goods
		double total = 0;
		for (CarVo car : buyVo.getList()){
			Goods tmp = goodsDao.findById(car.getGoods().getId());
			if(tmp.getStock()< car.getCount()){
				return "库存不足"; //库存不足
			}
			total += tmp.getPrice() * car.getCount(); //计算总金额
			//根据商品编号，扣减库存==》待完成
			goodsDao.modifyStock(tmp.getId(), tmp.getStock()-car.getCount());
			//添加订单详情表数据
			OrderInfo info = new OrderInfo();
			info.setAmount(tmp.getPrice());//单价
			info.setBuyNum(car.getCount()); //数量
			info.setGoodsId(tmp.getId()); //商品编号
		}
		//判断支付方式，是否为余额扣款 ==》t_user
		User user = userDao.findUserByAccount(buyVo.getAccount());
		if(buyVo.getPayType() == Integer.valueOf(Constants.PayType.USER_MONEY_PAY)) {
			if (user.getMoney() < total) {
				return "余额不足";//
			}
			//修改用户账户里的钱
			double money = user.getMoney() - total; //剩余
			//修改==》待完成
			userDao.modifyMoney(user.getId(), money);
		}
		//创建订单表==>t_base_order
		BaseOrder order = new BaseOrder();
		order.setAccount(user.getAccount());
		order.setUserId(user.getId());
		order.setAmount((float)total);
		order.setCreateTime(LocalDateTime.now());
		order.setOrderNo(buyVo.getOrderNo());
		//添加订单信息==》新增数据==》待完成
		BaseOrderDao baseOrderDao = new BaseOrderDaoImpl();
		baseOrderDao.add(order);
		//创建订单详情==》t_order_info
		//遍历详表==》循环添加到数据库==》待完成
		OrderInfoDao infoDao = new OrderInfoDaoImpl();
		for(OrderInfo tmp : infoList){
			tmp.setBaseOrderId(order.getId());  //主表==》订单编号
			//添加==》待完成
			infoDao.add(tmp);
		}
		return null;
	}
}
